/**   
*    
* 项目名称：SmartKungFuService   
* 类名称：ResetPassServlet   
* 类描述：   修改密码
* 创建人：zhanghl   
* 创建时间：2016年10月28日 下午6:20:56   
* 修改人：zhanghl   
* 修改时间：2016年10月28日 下午6:20:56   
* 修改备注：   
* @version    
*    
*/
package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.ClientReqPara;
import com.lst.entity.response.ClientResPara;
import com.lst.model.MstClient;
import com.lst.service.MstClientService;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: ResetPassServlet
 * @Description: 修改密码
 * @author zhanghl
 * @date 2016年10月28日 下午6:20:56
 *
 */
public class ResetPassServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	private MstClientService mstClientService = ctx.getBean(MstClientService.class); 

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();
		logger.info("doPost begin: " + startDate);

		ClientResPara resPara = new ClientResPara();

		try {
			ClientReqPara reqPara = getReqPara(request);

			if(isCheckField(reqPara,resPara)){
				Integer clientid = Integer.valueOf(reqPara.getUserid());
				String password = reqPara.getPassword();
                String repassword = reqPara.getRepassword();
                
                MstClient mstclient = new MstClient();

				MstClient mc = mstClientService.selectByPrimaryKey(clientid);
				if(mc!=null){
					if(!mc.getPassword().equals(password)){
						resPara.setCode(CommCode.M_A000000);
						resPara.setMessage(CommCode.M_C000010);
					}else if(password.equals(repassword)){
						resPara.setCode(CommCode.M_A000000);
						resPara.setMessage(CommCode.M_C000026);
					}else{
						mstclient.setPassword(repassword);
						mstclient.setUpdateuser(clientid);
						mstclient.setUpdatedate(startDate);
						mstclient.setId(clientid);
						
		                mstClientService.updateByPrimaryKeySelective(mstclient);
						
		                MstClient mcl = mstClientService.selectByPrimaryKey(clientid);
		                
						resPara.setCode(CommCode.M_Y000000);
						resPara.setMessage(CommCode.M_Y000001);
						resPara.setMstClient(mcl);	
					}
				}else{
					resPara.setCode(CommCode.M_Y000000);
					resPara.setMessage("未绑定手机,未设置密码");
				}
				
			}

		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}

		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);

			logger.info("ResetPasswordServlet jsonResult: " + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

			logger.info("doPost end: " + DateUtil.calLastedTime(startDate));

		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public ClientReqPara getReqPara(HttpServletRequest request) {

		ClientReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new ClientReqPara());

			reqPara = (ClientReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
			String password = request.getParameter("password");
			String repassword = request.getParameter("repassword");

			logger.info("reqPara userid: " + userid);
            logger.info("reqPara password: " + password);
            logger.info("reqPara repassword: " + repassword);
            
			reqPara.setUserid(userid);	
			reqPara.setPassword(password);
			reqPara.setRepassword(repassword);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		ClientReqPara reReqPara = (ClientReqPara) reqPara;
		ClientResPara reResPara = (ClientResPara) resPara;

	    if (StringUtils.isEmpty(reReqPara.getUserid())){
	    	reResPara.setCode(CommCode.M_A000000);
			reResPara.setMessage(CommCode.M_P000002);
	    } else if (StringUtils.isEmpty(reReqPara.getPassword())){		
			reResPara.setCode(CommCode.M_A000000);
			reResPara.setMessage(CommCode.M_C000009);
		} else if (StringUtils.isEmpty(reReqPara.getRepassword())){
			reResPara.setCode(CommCode.M_A000000);
			reResPara.setMessage(CommCode.M_C000008);
		} else{
			flag = true;
		}
		return  flag;
	}
}
